const mongoose = require('mongoose');
const config = require('../../config');
const db = config.database;

exports.connect = (success) => {
  mongoose.connect(`mongodb://${db.host}:${db.port}/${db.database}`, { useNewUrlParser: true }, function(err) {
      if (err) {
        console.log('数据库链接出错', err);
      } else {
        console.log('数据库链接成功');
      }
  });  // 不需要用户名密码
  // mongoose.connect(`mongodb://${db.user}:${db.password}@${db.host}:${db.port}/${db.database}`);  // 需要用户名密码
  const connection = mongoose.connection;
  connection.on('error', console.error.bind(console, 'connection error:'));
  connection.once('open', () => {
    console.log(`${db.database}数据库连接成功`);
    if (success) {
      success();
    }
  });
}

/*
  nodejs模块小知识
  exports.xxx 用于暴露方法或属性
  module.exports = xxx 用于暴露对象
 */
